package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.ProductDTO;
import com.financing.wap.dto.ProjectCompanyDTO;
import com.financing.wap.service.IProjectCompanyService;
import com.financing.wap.web.controller.BaseController;

@Controller("apiProductCompanyController")
@RequestMapping("/api/productCompany")
public class ProjectCompanyController extends BaseController {

	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IProjectCompanyService projectCompanyServiceImpl;

	/**
	 * 展示所有产品公司列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/productlist")
	@ResponseBody
	public ResultMap productlist(HttpServletRequest request, HttpServletResponse respons,ProjectCompanyDTO projectCompanyDTO,Pager pager){
		log.info("In ProductCompanyController productlist begin!");

		ResultMap result = new ResultMap();
		try{
			boolean isTrust = projectCompanyDTO.getId() != null && projectCompanyDTO.getId() >= 0;
			boolean isOrther = StringUtils.isNotBlank(projectCompanyDTO.getcName());
			//既非信托 又非其他  则参数缺失
			if (!isTrust&&!isOrther) {
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setMessage("参数缺失");
				return result;
			}
			
			Map<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("pager", pager);
			if(isTrust){
				queryParams.put("pInvestOwnerId", projectCompanyDTO.getId());
			}else if(isOrther){
				queryParams.put("pInvestOwner", projectCompanyDTO.getcName());
			}
			queryParams.put("user",getSession(true).getUser());
			
			//查询固定的产品类型
			String pType = request.getParameter("pType");
			if(StringUtils.isNotBlank(pType)){
				queryParams.put("pType", pType);
			}
			
			log.info("In ProductCompanyController productlist parameter id:"+projectCompanyDTO.getId());
			
			int count = projectCompanyServiceImpl.getCompanyProductListCount(queryParams);
			pager.setTotalCount(count);
			
			List<ProductDTO> list;
			if(count>0){
				list = projectCompanyServiceImpl.getCompanyProductList(queryParams);
			}else{
				list = Collections.EMPTY_LIST;
			}
			
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setAttribute("list", list);
			result.setAttribute("pager", pager);
			
		log.info("In ProductCompanyController productlist end!");
			return result;
		}catch(Exception e){
			log.error("error ProductCompanyController productlist:",e);
			result.setStatus(ResultMap.STATUS.WARN);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			return result;
		}
	}

	/**
	 * 展示所有产品公司列表
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, HttpServletResponse respons,ProjectCompanyDTO projectCompanyDTO){
		
		log.info("In ProductCompanyController list begin!");

		ResultMap result = new ResultMap();
		try{
			List<ProjectCompanyDTO> list = projectCompanyServiceImpl.getProjectCompanyList(projectCompanyDTO);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setAttribute("list", list);
		log.info("In ProductCompanyController list end!");
			return result;
		}catch(Exception e){
			log.error("error ProductCompanyController list:", e);
			result.setStatus(ResultMap.STATUS.WARN);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			return result;
		}
	}
	@RequestMapping("/view/{id}")
	@ResponseBody
	public ResultMap view(HttpServletRequest request, HttpServletResponse respons,@PathVariable Integer id){
		log.info("In ProductCompanyController view begin!");

		ResultMap result = new ResultMap();
		try{
		log.info("In ProductCompanyController view id:"+id);
			ProjectCompanyDTO list = projectCompanyServiceImpl.getProjectCompany(id);
			
			if(list==null){
				result.setStatus(ResultMap.STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_OBJECT_NOFOUNT);
				return result;
			}
			
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setAttribute("dto", list);
		log.info("In ProductCompanyController list end!");
			return result;
		}catch(Exception e){
			log.error("error ProductCompanyController list:", e);
			result.setStatus(ResultMap.STATUS.WARN);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			return result;
		}
	}
}
